package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.java.business.model.PurchaseBill;
//import com.java.business.service.api.PurchaseBillServiceApi;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.dao.AccessoriesMapper;
import com.java.manage.model.Accessories;
import com.java.manage.model.AccessoriesType;
import com.java.manage.model.Company;
import com.java.manage.model.Maintain;
import com.java.manage.service.AccessoriesService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AccessoriesServiceImpl implements AccessoriesService {
    @Autowired
    private AccessoriesMapper accessoriesMapper;
//    @DubboReference
//    private PurchaseBillServiceApi purchaseBillServiceApi;

    @Autowired
    private HttpServletRequest request;
    @Override
    public Result save(Accessories accessories) {
        accessories.setStatus("1");
        accessories.setPurchase("0");
        accessories.setUsedTime(new Date());
        accessories.setBuyedTime(new Date());
        accessoriesMapper.save(accessories);
        return Result.success();
    }
    @Override
    public Result modify(Accessories accessories) {
        accessoriesMapper.modify(accessories);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            accessoriesMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Accessories accessories) {
        PageHelper.startPage(accessories.getPageNum(), accessories.getPageSize());

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        accessories.setStoreId(storeId);

        List<Accessories> list = accessoriesMapper.list(accessories);
        PageInfo<Accessories> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }


    @Override
    public Result get(int id) {
        return null;
    }

    @Override
    public Result listSupply() {
        List<Accessories> companies = accessoriesMapper.listSupply();
        return Result.success(companies);
    }

    @Override
    public Result change(Integer id) {
        accessoriesMapper.change(id);
        return Result.success();
    }
    @Override
    public Result replace(Integer id) {
        accessoriesMapper.replace(id);
        return Result.success();
    }
}
